//
//  UploadShopImageController.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/12/12.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "UploadShopImageController.h"
#import "WShopUploadView.h"
#import "LTTextView.h"
#import "UploadImageCell.h"
#import "HXPhotoViewController.h"
#import "HXPhotoView.h"
static const CGFloat kPhotoViewMargin = 6.0;

@interface UploadShopImageController ()<HXPhotoViewDelegate,UIImagePickerControllerDelegate>

@property (strong, nonatomic) HXPhotoManager *manager;
@property (strong, nonatomic) HXPhotoView *photoView;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *imageArray;

@property (nonatomic, weak) WShopUploadView *nameView;
@property (nonatomic, weak) WShopUploadView *priceView;
@property (nonatomic, weak) WShopUploadView *linkView;

@property (nonatomic, weak) UILabel *labellllll;


@end

@implementation UploadShopImageController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WhiteColor;
    
    UIButton *popButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [popButton setImage:[UIImage imageNamed:@"aadelete"] forState:UIControlStateNormal];
    [self.view addSubview:popButton];
    [popButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Margin);
        make.top.mas_equalTo(Height_StatusBar+20);
        make.size.mas_equalTo(CGSizeMake(jisuan(36), jisuan(36)));
    }];
    [popButton addTarget:self action:@selector(pobuttonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *zailabel = [[UILabel alloc] init];
    zailabel.text = @"在爱淘帮";
    zailabel.preferredMaxLayoutWidth = WIDTH;//给一个maxWidth
    [zailabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    zailabel.textColor = Color999999;
    zailabel.font = [UIFont fontWithName:@"PingFang-SC" size:jisuan(40)];
    [self.view addSubview:zailabel];
    [zailabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Margin+5);
        make.top.equalTo(popButton.mas_bottom).offset(jisuan(70));
    }];
    
    UILabel *tianLabel = [[UILabel alloc] init];
    tianLabel.text = @"填写我想要的商品";
    tianLabel.preferredMaxLayoutWidth = WIDTH;//给一个maxWidth
    [tianLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    tianLabel.textColor = Color333333;
    tianLabel.font = [UIFont systemFontOfSize:jisuan(40)];
    [self.view addSubview:tianLabel];
    [tianLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Margin+5);
        make.top.equalTo(zailabel.mas_bottom).offset(2);
    }];
    
    UIImageView *imagelo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tuoyuan"]];
    [self.view addSubview:imagelo];
    [imagelo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tianLabel.mas_right).offset(10);
        make.centerY.equalTo(zailabel.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(jisuan(155), jisuan(100)));
    }];
    
    WShopUploadView *nameView = [[WShopUploadView alloc] initWithTitle:@"商品名称"];
    nameView.textView.placeholderTextView.text = @"（必填）";
    nameView.textView.placeholderTextView.textColor = Colorff0000;
    [self.view addSubview:nameView];
    [nameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(tianLabel.mas_bottom).offset(jisuan(110));
        make.height.mas_equalTo(jisuan(110));
    }];
    
    WShopUploadView *pricView = [[WShopUploadView alloc] initWithTitle:@"商品价格"];
    [self.view addSubview:pricView];
    [pricView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(nameView.mas_bottom);
        make.height.mas_equalTo(jisuan(120));
    }];
    
    WShopUploadView *lianjieView = [[WShopUploadView alloc] initWithTitle:@"商品链接"];
    [self.view addSubview:lianjieView];
    [lianjieView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(pricView.mas_bottom);
        make.height.mas_equalTo(jisuan(120));
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = Colorff0000;
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.equalTo(lianjieView.mas_bottom).offset(10);
        make.width.mas_equalTo(2);
        make.height.mas_equalTo(jisuan(30));
    }];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"商品图片";
    [nameLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    nameLabel.textColor = Color333333;
    nameLabel.font = Font(30);
    [self.view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_right).offset(10);
        make.centerY.equalTo(line.mas_centerY);
    }];

    UILabel *labellll = [[UILabel alloc] init];
    labellll.text = @"（选填）";
    labellll.font = [UIFont fontWithName:@"PingFangSC-Thin" size:14];
    labellll.textColor = Colordddddd;
    [self.view addSubview:labellll];
    [labellll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameLabel.mas_centerY);
        make.right.mas_equalTo(-10);
        make.left.equalTo(nameLabel.mas_right).offset(5);
        make.bottom.mas_equalTo(-0.5);
    }];

    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self.view layoutIfNeeded];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(lianjieView.frame)+jisuan(120), WIDTH, 130)];
    scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;

    
    CGFloat width = scrollView.frame.size.width;
    HXPhotoView *photoView = [HXPhotoView photoManager:self.manager];
    photoView.frame = CGRectMake(kPhotoViewMargin, kPhotoViewMargin, width - kPhotoViewMargin * 2, 0);
    photoView.delegate = self;
    photoView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:photoView];
    self.photoView = photoView;
    

    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureButton setBackgroundImage:[UIImage imageNamed:@"juxing"] forState:UIControlStateNormal];
    [sureButton setTitle:@"确认提交" forState:UIControlStateNormal];
    sureButton.titleLabel.font = Font(32);
    [self.view addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.mas_equalTo(-Scale_W(20));
        make.size.mas_equalTo(CGSizeMake(jisuan(550), jisuan(80)));
    }];
    [sureButton addTarget:self action:@selector(surebuttonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *labeall = [[UILabel alloc] init];
    labeall.text = @"最多可上传3张照片";
    labeall.font = [UIFont fontWithName:@"PingFangSC-Thin" size:14];
    labeall.textColor = Colordddddd;
    [self.view addSubview:labeall];
    [labeall mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(scrollView.mas_centerY);
        make.left.mas_equalTo(140);
    }];
    self.labellllll = labeall;
    
    self.nameView = nameView;
    self.priceView = pricView;
    self.linkView = lianjieView;
    
}

- (void)surebuttonClick {
    
    if (self.nameView.textView.textView.text.length < 1) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请填写商品名称"];
        return;
    }
    NSString *name = self.nameView.textView.textView.text;
    NSString *price = self.priceView.textView.textView.text.length < 0 ? @"" : self.priceView.textView.textView.text;
    NSString *link = self.linkView.textView.textView.text.length < 0 ? @"" : self.linkView.textView.textView.text;

    
    NSDictionary *dic = @{@"user_id":[CommonUserInfo sharedInstance].user_id, @"title":name, @"money":price, @"link":link};
    NSString *url = [NSString stringWithFormat:@"%@%@",ebaseURL,@"/user/Want/goods"];
//    @"http://shop.quxiangtao.com/qxt.php/user/Want/goods"
    [PPNetworkHelper uploadWithURL:url parameters:dic images:self.imageArray name:@"img[]" fileName:@"aaaa" mimeType:@"jpg" progress:^(NSProgress *progress) {
        
    } success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"data"]];
        } else {
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"data"]];
        }
    } failure:^(NSError *error) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"提交失败"];
    }];
    
}


- (HXPhotoManager *)manager
{
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.openCamera = YES;
        _manager.outerCamera = YES;
        _manager.showFullScreenCamera = YES;
        _manager.photoMaxNum = 3 ;
        _manager.videoMaxNum = 0;
        _manager.maxNum = 8;
    }
    return _manager;
}


- (void)photoView:(HXPhotoView *)photoView changeComplete:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photos videos:(NSArray<HXPhotoModel *> *)videos original:(BOOL)isOriginal {
    WS(weakSelf);
    self.imageArray = nil;
    self.labellllll.hidden = photos.count > 0 ? YES : NO;
    NSSLog(@"所有:%ld - 照片:%ld - 视频:%ld",allList.count,photos.count,videos.count);
    [HXPhotoTools getImageForSelectedPhoto:photos type:HXPhotoToolsFetchHDImageType completion:^(NSArray<UIImage *> *images) {
        weakSelf.imageArray = images;
        
    }];
    
}

- (void)photoView:(HXPhotoView *)photoView updateFrame:(CGRect)frame {
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, CGRectGetMaxY(frame) + kPhotoViewMargin);
}



- (void)pobuttonClick {
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
}



@end
